<?php
class jurisdicciont extends Controller 
{
	function __construct()
	{
		parent::Controller();
		$this->load->database();
		$this->load->model('mjurisdiccion');
	    $this->load->model('mcompania');

	}
	
	function index()
	{
		$this->load->view('main', array('content_page' => 'jurisdiccion_list','menutop' => 0));
	}
	
	function add()
	{
		$this->load->view('main', array('content_page' => 'jurisdiccion_add','menutop' => 0));
	}
	
	function edit($var)
	{
		$nombre = $this->mjurisdiccion->LoadjurisdiccionById($var);
		$this->load->view('main', array('content_page' => 'jurisdiccion_edit','menutop' => 0,'jur'=>$nombre));
	}
	
	function delete() 
	{
			$ids = $this->input->post('delete_rows');
			
			if(strpos($ids,"|") === false) 
			{ 
				$num = $this->mcompania->LoadCompanyCantidad('IdJurisdiccion',$ids);
				if ($num == 0) {
			   
					 if($this->mjurisdiccion->Delete($ids) !== false) 
				     {
					   echo 'El Registro fue eliminado satisfactoriamente';
				      } 
				     else 
				     {
						echo 'Hubo un error eliminando el Registro.  Intentelo nuevamente mas tarde 						o comuniquese con el administrador';
				     }
				
				}
				
				else {
					
					echo 'Existe usuarios activos en el departamento';
				}
			
				
			} 
		
	}
	
	function data() 
	{
		$page = $this->input->post('page') ? $this->input->post('page') : 1; // Pagina Actual o 1 por default
		$sortname = $this->input->post('sidx') ? $this->input->post('sidx') : 'ID_airport'; // Columna para el order by
		$sortorder = $this->input->post('sord') ? $this->input->post('sord') : 'asc'; // Orden (asc|desc)
		$limit = $this->input->post('rows') ? $this->input->post('rows') : 10;
		$start = $limit * $page - $limit;
		if($start < 0) $start = 0;

		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		header("Cache-Control: no-cache, must-revalidate" );
		header("Pragma: no-cache" );

		$records = $this->mjurisdiccion->LoadJurisdiccion($this->input->post('name'), $start, $limit, $sortname, $sortorder);

		$totalRecords = $this->mjurisdiccion->GetCount($this->input->post('name'));

		$totalPages = ceil($totalRecords/$limit);
		if ($page > $totalPages) $page = $totalPages;

		$json_output = array(
		'page'		=> $page,
		'total'		=> $totalPages,
		'records'	=> $totalRecords,
		'rows'		=> array()
		);

		if($records != false) {
			foreach($records as $jur) {
				$arr = $json_output['rows'][] = array(
				'IdJurisdiccion' => $jur->getIdJurisdiccion(),
				'NombreJurisdiccion' => $jur->getNombreJurisdiccion());
			}
		}

		echo json_encode($json_output);
	}
	
	function save()
	{
		$nombre = $this->input->post('nombre');
      if ($this->input->post('id') < 1 || $this->input->post('id') == false) {
              if ($this->mjurisdiccion->createJurisdiccion($nombre)=== false) {
                	
              	$this->session->set_flashdata('usuario','La juridiccion fue creada');
                }  
	             else  
	             {
	   	         $this->session->set_flashdata('usuario','El usuario ya existe vuelva a intentar con uno diferente');
	   	redirect('jurisdicciont');  
	   }
	   redirect('jurisdicciont');  
		
	   }
	   else{
	   	 
	       $this->mjurisdiccion->updateJurisdiccion($nombre,$this->input->post('id'));	
	    redirect('jurisdicciont');  
	   	
	   }
	   
	}

	
}